<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon.ico?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon.ico?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="JavaScript,">










<meta name="description" content="JavaScript引用类型 先来说说 JavaScript 的数据类型吧 基本类型：undefined, null, boolean, string, symbol, number  普通基本类型：undefined, null, symbol  基本包装类型：Boolean,String,Number (基本包装类型的构造函数typeof检测会返回Object) 123456789101112">
<meta name="keywords" content="JavaScript">
<meta property="og:type" content="article">
<meta property="og:title" content="一些引用类型">
<meta property="og:url" content="http://zhanglei_c.gitee.io/blog/2019/10/15/一些引用类型/index.html">
<meta property="og:site_name" content="爱吃零食的猫">
<meta property="og:description" content="JavaScript引用类型 先来说说 JavaScript 的数据类型吧 基本类型：undefined, null, boolean, string, symbol, number  普通基本类型：undefined, null, symbol  基本包装类型：Boolean,String,Number (基本包装类型的构造函数typeof检测会返回Object) 123456789101112">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-10-30T04:55:04.478Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="一些引用类型">
<meta name="twitter:description" content="JavaScript引用类型 先来说说 JavaScript 的数据类型吧 基本类型：undefined, null, boolean, string, symbol, number  普通基本类型：undefined, null, symbol  基本包装类型：Boolean,String,Number (基本包装类型的构造函数typeof检测会返回Object) 123456789101112">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://zhanglei_c.gitee.io/blog/2019/10/15/一些引用类型/">





  <title>一些引用类型 | 爱吃零食的猫</title>
  








</head>

<script type="text/javascript" color="0,0,255" opacity="0.7" zindex="-2" count="99" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
<a href="https://github.com/403NotFound" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">爱吃零食的猫</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">自由，平等，公正，法治</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://zhanglei_c.gitee.io/blog/blog/2019/10/15/一些引用类型/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Zhang_lei">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱吃零食的猫">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">一些引用类型</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-15T22:07:01+08:00">
                2019-10-15
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="JavaScript引用类型"><a href="#JavaScript引用类型" class="headerlink" title="JavaScript引用类型"></a>JavaScript引用类型</h2><blockquote>
<p>先来说说 JavaScript 的数据类型吧</p>
<p><strong>基本类型</strong>：undefined, null, boolean, string, symbol, number</p>
<ul>
<li><p>普通基本类型：undefined, null, symbol</p>
</li>
<li><p>基本包装类型：Boolean,String,Number <strong>(基本包装类型的构造函数typeof检测会返回Object)</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&gt;   <span class="keyword">var</span> num = <span class="keyword">new</span> <span class="built_in">Number</span>()</span><br><span class="line">&gt;   <span class="keyword">var</span> _num = <span class="number">1</span></span><br><span class="line">&gt;   <span class="keyword">var</span> str = <span class="keyword">new</span> <span class="built_in">String</span>()</span><br><span class="line">&gt;   <span class="keyword">var</span> _str = <span class="string">'red'</span></span><br><span class="line">&gt;   <span class="keyword">var</span> bol = <span class="keyword">new</span> <span class="built_in">Boolean</span>()</span><br><span class="line">&gt;   <span class="keyword">var</span> _bol = <span class="literal">true</span></span><br><span class="line">&gt;   </span><br><span class="line">&gt;   <span class="keyword">typeof</span>(num) <span class="comment">// Object</span></span><br><span class="line">&gt;   <span class="keyword">typeof</span>(_num) <span class="comment">// number</span></span><br><span class="line">&gt;   <span class="keyword">typeof</span>(str) <span class="comment">// Object</span></span><br><span class="line">&gt;   <span class="keyword">typeof</span>(_str) <span class="comment">// string</span></span><br><span class="line">&gt;   <span class="keyword">typeof</span>(bol) <span class="comment">// Object</span></span><br><span class="line">&gt;   <span class="keyword">typeof</span>(_bol) <span class="comment">// boolean</span></span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</li>
</ul>
</blockquote>
<blockquote>
<p><strong>引用类型</strong>：Object, Array, RegExp, Data, Function</p>
</blockquote>
<h3 id="1-Function类型"><a href="#1-Function类型" class="headerlink" title="1. Function类型"></a>1. Function类型</h3><h4 id="1-1-函数内部属性"><a href="#1-1-函数内部属性" class="headerlink" title="1.1 函数内部属性"></a>1.1 函数内部属性</h4><ul>
<li>函数名是一个指向函数对象的指针</li>
</ul>
<blockquote>
<p>函数重载，在C++等高级语言中函数重载指的是两个以上具有相同函数名，但是形参或参数类型不同，函数在调用的时候根据参数的不同选择不同的函数进行调用</p>
<p><strong>JavaScript中没有函数重载这一概念，同名函数会被覆盖</strong></p>
</blockquote>
<ul>
<li><p>函数内部属性</p>
<ul>
<li><p><code>arguments</code> 伪数组，包含着传入函数内的所有参数</p>
<ul>
<li><code>callee</code> 是 <code>arguments</code> 的一个内部属性</li>
<li>是一个指针，指向拥有这个<code>arguments</code>对象的函数</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">factorial</span>(<span class="params">num</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(num &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> num * factorial(num - <span class="number">1</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 等价于</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">factorial</span>(<span class="params">num</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(num &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> num * <span class="built_in">arguments</span>.callee(num - <span class="number">1</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><code>this</code>  函数执行的环境对象</p>
</li>
</ul>
</li>
</ul>
<h4 id="1-2-函数属性和方法"><a href="#1-2-函数属性和方法" class="headerlink" title="1.2 函数属性和方法"></a>1.2 函数属性和方法</h4><ul>
<li><p><code>length</code>函数希望接收的命名参数的个数</p>
</li>
<li><p><code>prototype</code>函数的原型对象，保存所有的实例方法</p>
<ul>
<li><p><code>apply()</code></p>
</li>
<li><p><code>call()</code></p>
</li>
</ul>
<blockquote>
<p><code>apply()</code> 接收两个参数，一个是其中运行函数的作用域，另一个是参数数组或者<code>arguments</code>对象</p>
<p><code>call()</code> 接收的第一个参数也是函数的作用域，剩下的参数是当前函数的参数,<strong>必须明确地传入每一个参数</strong></p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">num1,num2</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> num1 + num2</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// call</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">callSum</span>(<span class="params">num1,num2</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> sum.call(<span class="keyword">this</span>,num1,num2)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// apply</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">callSum2</span>(<span class="params">num1,num2</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> sum.apply(<span class="keyword">this</span>,<span class="built_in">arguments</span>) <span class="comment">// or sum.apply(this,[num1,num2])</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// ****************************************************************************</span></span><br><span class="line"><span class="comment">// apply 和 call 真正厉害的地方在于拓展函数的作用域</span></span><br><span class="line"><span class="built_in">window</span>.color = <span class="string">'red'</span></span><br><span class="line"><span class="keyword">var</span> o = &#123;<span class="attr">color</span>: <span class="string">'blue'</span>&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sayColor</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.color</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">sayColor() <span class="comment">// red</span></span><br><span class="line">sayColor.call(<span class="keyword">this</span>) <span class="comment">// red</span></span><br><span class="line">sayColor.call(<span class="built_in">window</span>) <span class="comment">// red</span></span><br><span class="line">sayColor.call(o) <span class="comment">// blue</span></span><br><span class="line">sayColor.bind(o) <span class="comment">// blue</span></span><br></pre></td></tr></table></figure>
<ul>
<li><code>bind()</code> ES5的方法，创建一个实例，this值会被绑定到传给bind()函数的值</li>
</ul>
</li>
</ul>
<h4 id="1-3-基本包装类型"><a href="#1-3-基本包装类型" class="headerlink" title="1.3 基本包装类型"></a>1.3 基本包装类型</h4><blockquote>
<p>Boolean, String, Number 这三种特殊的引用类型被称为基本包装类型</p>
<p><strong>特性：基本包装类型的构造函数用 <code>typeof</code>检测会返回 Object</strong></p>
</blockquote>
<h4 id="1-4-Boolean-类型"><a href="#1-4-Boolean-类型" class="headerlink" title="1.4 Boolean 类型"></a>1.4 Boolean 类型</h4><blockquote>
<p>布尔表达式中的所有对象都会被转换为<code>true</code></p>
<p>例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; <span class="keyword">var</span> bool = <span class="keyword">new</span> <span class="built_in">Boolean</span>(<span class="literal">false</span>)</span><br><span class="line">&gt; bool &amp;&amp; <span class="literal">true</span> <span class="comment">// true</span></span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<h4 id="1-5-Number-类型"><a href="#1-5-Number-类型" class="headerlink" title="1.5 Number 类型"></a>1.5 Number 类型</h4><ul>
<li><p>方法</p>
<ul>
<li><code>toString()</code><ul>
<li>不传参 将数字转化为字符串</li>
<li>传参 传入的数字是几就会返回对应的几进制字符串形式</li>
</ul>
</li>
<li><code>toFixed()</code> 会按照指定的小数位返回对应的字符串形式，会四舍五入</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> num = <span class="number">10</span></span><br><span class="line">num.toFixed(<span class="number">2</span>) <span class="comment">// "10.00"</span></span><br><span class="line"><span class="keyword">var</span> _num = <span class="number">10.005</span></span><br><span class="line">_num.toFixed(<span class="number">2</span>) <span class="comment">// "10.01"</span></span><br></pre></td></tr></table></figure>
<ul>
<li>…</li>
</ul>
</li>
</ul>
<h4 id="1-6-String-类型"><a href="#1-6-String-类型" class="headerlink" title="1.6 String 类型"></a>1.6 String 类型</h4><blockquote>
<p>String 类型的每一个实例都有 length 属性，返回字符串的字符个数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; <span class="keyword">var</span> str = <span class="string">"hello world"</span></span><br><span class="line">&gt; str.length <span class="comment">// 11</span></span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<ul>
<li><p>字符方法</p>
<ul>
<li><code>slice()</code></li>
<li><code>substring()</code><ul>
<li><strong>两个参数时，会默认将小的那个数作为起始位置</strong></li>
</ul>
</li>
<li><code>substr()</code></li>
</ul>
<blockquote>
<p>1.接收一个参数时 ，都返回当前参数的位置到最后的所有字符（会以字符串长度作为第二参数）</p>
<p>2.接收两个参数时，slice() 和 substring() 都会以第二个参数的位置作为结束位置，substr()的第二个参数的意义是要返回的字符个数</p>
<p>3.接收一个负数时</p>
<ul>
<li><code>slice()</code>将负数与字符串长度相加，得到的正数作为参数</li>
<li><code>substring()</code>将负数转换成 0</li>
<li><code>substr()</code>和<code>slice()</code>一样</li>
</ul>
<p>4.接收两个参数时（第二个为负数）</p>
<ul>
<li><code>slice（）</code>同上</li>
<li><code>substring()</code> 同上</li>
<li><code>substr()</code> 将第二个参数置0</li>
</ul>
</blockquote>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">"hello world"</span> <span class="comment">// length 11</span></span><br><span class="line"></span><br><span class="line">str.slice(<span class="number">3</span>) <span class="comment">// "lo world"</span></span><br><span class="line">str.slice(<span class="number">-3</span>) <span class="comment">// str.slice(8) ==&gt; "rld"</span></span><br><span class="line">str.slice(<span class="number">3</span>,<span class="number">-4</span>) <span class="comment">// str.slice(3,7) ==&gt; "lo w"</span></span><br><span class="line">str.slice(<span class="number">3</span>,<span class="number">7</span>) <span class="comment">// "lo w"</span></span><br><span class="line"></span><br><span class="line">str.substring(<span class="number">3</span>) <span class="comment">// "lo world"</span></span><br><span class="line">str.substring(<span class="number">-3</span>) <span class="comment">// str.substring(0) ==&gt; "hello world"</span></span><br><span class="line">str.substring(<span class="number">3</span>,<span class="number">-4</span>) <span class="comment">// str.substring(3,0) ==&gt; "hel"</span></span><br><span class="line">str.substring(<span class="number">3</span>,<span class="number">7</span>) <span class="comment">// "lo w"</span></span><br><span class="line"></span><br><span class="line">str.substr(<span class="number">3</span>) <span class="comment">// "lo world"</span></span><br><span class="line">str.substr(<span class="number">-3</span>) <span class="comment">// str.substr(8) ==&gt; "rld"</span></span><br><span class="line">str.substr(<span class="number">3</span>,<span class="number">-4</span>) <span class="comment">// str.substr(3,0) ==&gt; ""(空字符串)</span></span><br><span class="line">str.substr(<span class="number">3</span>,<span class="number">7</span>) <span class="comment">// "lo worl"</span></span><br></pre></td></tr></table></figure>
<ul>
<li><p>位置方法</p>
<blockquote>
<p>没有找到就返回 -1</p>
<p>可以接收两个参数，第一个参数是要查询的字符串，第二个参数是从何处开始寻找</p>
</blockquote>
<ul>
<li><code>indexOf()</code> 从前往后</li>
<li><code>lastIndexOf()</code> 从后往前</li>
<li><code>trim()</code>  用于删除字符串前后多余的空格</li>
</ul>
</li>
<li><p>大小写转换</p>
<ul>
<li><code>toUpperCase()</code>  转大写</li>
<li><code>toLowerCase()</code>  转小写</li>
</ul>
</li>
<li><p>模式匹配</p>
<ul>
<li><p><code>match()</code>接收一个参数，正则表达式或一个RegExp对象，返回一个数组</p>
</li>
<li><p><code>search()</code>返回字符串中第一个匹配项中的索引，没有则返回-1</p>
</li>
<li><p><code>replace()</code>用于找出并替换指定字符串，接收两个参数</p>
<ul>
<li>第一个参数是字符串或正则表达式</li>
<li>第二个参数是要替换的字符串或一个函数</li>
<li>默认只替换第一个找到的指定字符串，若要替换所有的字符串，则需要提供正则表达式，并指定<strong>全局（g）标志</strong></li>
</ul>
</li>
<li><p><code>split()</code>可以基于指定的分隔符将一个字符串分割成多个子字符串，并把结果放入一个数组中</p>
<ul>
<li>第一个参数是分隔符</li>
<li>第二个参数代表返回数组的长度  </li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'red,blue,green'</span></span><br><span class="line">str.split(<span class="string">','</span>) <span class="comment">// ["red","blue","green"]</span></span><br><span class="line">str.split(<span class="string">','</span>,<span class="number">2</span>) <span class="comment">// ["red","blue"]</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<ul>
<li><p>字符编码</p>
<ul>
<li><code>fromCharCode()</code>接收字符编码转化为字符串</li>
<li><code>charCodeAt()</code>接收字符转换为字符编码</li>
</ul>
</li>
<li><p>遍历</p>
<ul>
<li><p><code>for...of</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">'abc'</span></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> code <span class="keyword">of</span> str) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(code) <span class="comment">// 'a','b','c'</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/JavaScript/" rel="tag"># JavaScript</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2019/10/15/js数组/" rel="next" title="js数组">
                <i class="fa fa-chevron-left"></i> js数组
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2019/10/18/面向对象和原型/" rel="prev" title="面向对象和原型">
                面向对象和原型 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Zhang_lei</p>
              <p class="site-description motion-element" itemprop="description">爱吃零食的猫</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/403NotFound" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/ai-chi-bao-mi-hua-de-mao/activities" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-send"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/ZhangLei_c" target="_blank" title="码云">
                      
                        <i class="fa fa-fw fa-github-alt"></i>码云</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5205419998/profile?rightmod=1&wvr=6&mod=personinfo&is_all=1" target="_blank" title="weibo">
                      
                        <i class="fa fa-fw fa-weibo"></i>weibo</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#JavaScript引用类型"><span class="nav-number">1.</span> <span class="nav-text">JavaScript引用类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-Function类型"><span class="nav-number">1.1.</span> <span class="nav-text">1. Function类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-函数内部属性"><span class="nav-number">1.1.1.</span> <span class="nav-text">1.1 函数内部属性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-函数属性和方法"><span class="nav-number">1.1.2.</span> <span class="nav-text">1.2 函数属性和方法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-基本包装类型"><span class="nav-number">1.1.3.</span> <span class="nav-text">1.3 基本包装类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-4-Boolean-类型"><span class="nav-number">1.1.4.</span> <span class="nav-text">1.4 Boolean 类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-5-Number-类型"><span class="nav-number">1.1.5.</span> <span class="nav-text">1.5 Number 类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-6-String-类型"><span class="nav-number">1.1.6.</span> <span class="nav-text">1.6 String 类型</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Zhang_lei</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>


<div>
<span id="busuanzi_container_site_pv">
  本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span id="busuanzi_container_site_uv">
    有<span id="busuanzi_value_site_uv"></span>人看过我的博客啦
</span>
</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
